<template>
  <view>
    <my-goods :goodList="goodList"></my-goods>
  </view>
</template>

<script>
  export default {
    data() {
      return {
        total: 0, //数据总数
        goodList: [], //数据集合
        query: '',
        cid: '',
        pagenum: 1, //获取第几页数据
        pagesize: 10, //获得几条数据
        loading: false
      };
    },
    methods: {
      async getGoodList(option, cb) {
        this.loading = true
        const {
          data: res
        } = await uni.$http.get('/api/public/v1/goods/search', {
          "query": option.query ? option.query : '',
          "cid": option.cid ? option.cid : '',
          "pagesize": this.pagesize,
          "pagenum": this.pagenum
        })
        cb && cb()
        this.loading = false
        if (res.meta.status !== 200) {
          return uni.$showMsg()
        }
        this.total = res.message.total
        this.goodList = [...this.goodList, ...res.message.goods]
      }
    },
    onPullDownRefresh() {
      const max = parseInt((this.total / this.pagesize).toFixed(0)) + 1
      this.pagenum = (this.pagenum % max) + 1
      this.goodList = []
      this.getGoodList({
        query: this.query,
        cid: this.cid
      }, () => uni.stopPullDownRefresh())
    },
    onReachBottom() {
      if (this.pagenum * this.pagesize >= this.total) {
        return uni.$showMsg("没有最新数据")
      }
      if (this.loading) return
      this.pagenum++
      this.getGoodList({
        query: this.query,
        cid: this.cid
      })
    },
    onLoad(option) {
      this.getGoodList(option)
      option.query ? this.query = option.query : this.query = ''
      option.cid ? this.cid = option.cid : this.cid = ''
    }
  }
</script>

<style lang="scss">

</style>